package com.slz3.business.module.drawManagement.draIntegralLottery.steward;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.enums.YNFlag;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.response.RR;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.drawManagement.draFreeLottery.domain.DraFreeLottery;
import com.slz3.business.module.drawManagement.draFreeLottery.domain.DraFreeLotteryCondition;
import com.slz3.business.module.drawManagement.draIntegralLottery.domain.DraIntegralLottery;
import com.slz3.business.module.drawManagement.draIntegralLottery.domain.DraIntegralLotteryCondition;
import org.springframework.web.reactive.function.server.ServerRequest;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

public class DraIntegralLotteryResSafeValid {

    static boolean add(ServerRequest req, DraIntegralLottery draIntegralLottery) {
        // T-O-D-O 唯一约束
        return RR.hasResErr(req);
    }

    /**
     * known: params uuids is not null
     */
    static final boolean delete(ServerRequest req, DraIntegralLotteryCondition draIntegralLotteryCondition) {
        List<DraIntegralLottery> draIntegralLotteryList = Services.draIntegralLottery.findAllByUuids(draIntegralLotteryCondition.getUuidList());
        if (draIntegralLotteryList.stream().filter(
                item -> AuditFlag.isApproved(item.getSerAudit())
                        || AuditFlag.isReject(item.getSerAudit())
                        || ValidFlag.isDel(item.getValidFlag())
                        || YNFlag.isYes(item.getSerAlreadyLotteryEnum())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能删除【已审核】或【被驳回】或【被删除】状态的记录 或 【已开奖】状态的记录");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, draIntegralLotteryList);
        }
        return RR.hasResErr(req);
    }

    static boolean modify(ServerRequest req, DraIntegralLottery draIntegralLottery) {
        Optional<DraIntegralLottery> draIntegralLotteryDB = Services.draIntegralLottery.single(draIntegralLottery.getUuid());
        if (draIntegralLotteryDB.isPresent()) {
            req.attributes().put(RR.RELAY_SAFE_VALID, draIntegralLotteryDB.get());
        } else {
            RR.resDetail(req).put(RR.SAFE_VALID, "cannot find entity by request uuid");
        }
        return RR.hasResErr(req);
    }

    static boolean info(ServerRequest req) {
        Optional<DraIntegralLottery> draIntegralLotteryDB = Services.draIntegralLottery.single(UUID.fromString(req.queryParam("uuid").get()));
        if (VE.isEmpty(draIntegralLotteryDB)) {
            RR.resDetail(req).put(RR.SAFE_VALID, "cannot find entity by request uuid");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, draIntegralLotteryDB.get());
        }
        return RR.hasResErr(req);
    }

    /**
     * known: params uuids is not null
     */
    static final boolean auditSubmit(ServerRequest req, DraIntegralLotteryCondition draIntegralLotteryCondition) {
        List<DraIntegralLottery> draIntegralLotteryList = Services.draIntegralLottery.findAllByUuids(draIntegralLotteryCondition.getUuidList());
        if (draIntegralLotteryList.stream().filter(item -> ValidFlag.isValid(item.getValidFlag())
                && (AuditFlag.isWaitSubmit(item.getSerAudit()) || AuditFlag.isReject(item.getSerAudit()))
        ).count() > 0) {
            req.attributes().put(RR.RELAY_SAFE_VALID, draIntegralLotteryList);
        } else {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能提交【被删除】状态或不是【待提交】或【驳回】状态的记录");
        }
        return RR.hasResErr(req);
    }

    /**
     * known: params uuids is not null
     */
    static final boolean audit(ServerRequest req, DraIntegralLotteryCondition draIntegralLotteryCondition) {
        List<DraIntegralLottery> draIntegralLotteryList = Services.draIntegralLottery.findAllByUuids(draIntegralLotteryCondition.getUuidList());
        if (draIntegralLotteryList.stream().filter(
                item -> !AuditFlag.isWaitAudit(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能审核【被删除】状态或不是【待审核】状态的记录");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, draIntegralLotteryList);
        }
        return RR.hasResErr(req);
    }

    /**
     * known: params uuids is not null
     */
    static final boolean auditReject(ServerRequest req, DraIntegralLotteryCondition draIntegralLotteryCondition) {
        List<DraIntegralLottery> draIntegralLotteryList = Services.draIntegralLottery.findAllByUuids(draIntegralLotteryCondition.getUuidList());
        if (draIntegralLotteryList.stream().filter(item -> ValidFlag.isValid(item.getValidFlag())
                && (AuditFlag.isWaitAudit(item.getSerAudit()) || AuditFlag.isApproved(item.getSerAudit()))
        ).count() > 0) {
            req.attributes().put(RR.RELAY_SAFE_VALID, draIntegralLotteryList);
        } else {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能驳回【被删除】状态或不是【待审核】或【已审核】状态的记录");
        }
        return RR.hasResErr(req);
    }


    static final boolean usingEnable(ServerRequest req, DraIntegralLotteryCondition draIntegralLotteryCondition) {
        List<DraIntegralLottery> draIntegralLotteryList = Services.draIntegralLottery.findAllByUuids(draIntegralLotteryCondition.getUuidList());
        if (draIntegralLotteryList.stream().filter(
                item -> !AuditFlag.isWaitAudit(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能启用【被删除】状态");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, draIntegralLotteryList);
        }
        return RR.hasResErr(req);
    }


    static final boolean usingDisable(ServerRequest req, DraIntegralLotteryCondition draIntegralLotteryCondition) {
        List<DraIntegralLottery> draIntegralLotteryList = Services.draIntegralLottery.findAllByUuids(draIntegralLotteryCondition.getUuidList());
        if (draIntegralLotteryList.stream().filter(
                item -> !AuditFlag.isWaitAudit(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能禁用【被删除】状态");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, draIntegralLotteryList);
        }
        return RR.hasResErr(req);
    }
    public static boolean lottery(ServerRequest req, DraIntegralLotteryCondition draIntegralLotteryCondition) {
        List<DraIntegralLottery> draIntegralLotteryList = Services.draIntegralLottery.findAllByUuids(draIntegralLotteryCondition.getUuidList());
        if (draIntegralLotteryList.stream().filter(item -> {
//            System.err.println(ValidFlag.isDel(item.getValidFlag()));
//            System.err.println(item.getSerGoodsDrawEndDate() == null);
//            System.err.println(item.getSerGoodsDrawNumber() == null);
//            System.err.println(item.getSerGoodsDrawEndDate().isAfter(LocalDateTime.now()));
//            System.err.println(YNFlag.isYes(item.getSerAlreadyLotteryEnum()));
//            System.err.println(item.getSerGoodsDrawNumber().length() == 0);
                    return ValidFlag.isDel(item.getValidFlag())
                            || item.getSerGoodsDrawEndDate() == null
//                || item.getSerAlreadyLotteryEnum() == null
                            || item.getSerGoodsDrawNumber() == null

                            || item.getSerGoodsDrawEndDate().isAfter(LocalDateTime.now())
                            || YNFlag.isYes(item.getSerAlreadyLotteryEnum())
                            || item.getSerGoodsDrawNumber().length() == 0;
                }
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能对【被删除】【未结束】【已开奖】和【无中奖号码】的记录进行开奖！");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, draIntegralLotteryList);
        }
        return RR.hasResErr(req);
    }
}
